package com.zyl.pagergalleryloadmoredemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zyl.pagergalleryloadmoredemo.R
import com.zyl.pagergalleryloadmoredemo.pojo.PhotoItem
import kotlinx.android.synthetic.main.cell_photo.view.*

class PhotoAdapter : ListAdapter<PhotoItem, PhotoViewHolder>(diffCallback) {
    object diffCallback : DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
        return PhotoViewHolder(
            LayoutInflater.from(parent.context).inflate((R.layout.cell_photo), parent, false)
        )
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        Glide.with(holder.itemView)
            .load(getItem(position).fullUrl)
            .placeholder(R.drawable.ic_baseline_photo_24)
            .into(holder.itemView.imageViewPhoto)
    }
}

class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)